export async function imageToBlob(image: HTMLImageElement, mimeType: string = "image/png"): Promise<Blob> {
  return new Promise((resolve, reject) => {
    const canvas = document.createElement("canvas");
    canvas.width = image.naturalWidth;
    canvas.height = image.naturalHeight;

    const ctx = canvas.getContext("2d");
    if (!ctx) {
      reject(new Error("无法获取 Canvas 上下文"));
      return;
    }

    ctx.drawImage(image, 0, 0);
    canvas.toBlob(blob => {
      if (blob) {
        resolve(blob);
      } else {
        reject(new Error("Canvas toBlob 转换失败"));
      }

      ctx.clearRect(0, 0, canvas.width, canvas.height);
    }, mimeType);
  });
}
